package cn.egeek.babel.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import cn.egeek.babel.entity.Topic;
import cn.egeek.babel.entity.User;
import cn.egeek.babel.service.CommentService;
import cn.egeek.babel.service.TopicService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;

@Results( { @Result(name = "success", type = "redirectAction", params = { "actionName", "topic" }) })
public class TopicController extends ValidationAwareSupport implements ModelDriven<Object>, Validateable {

	private Topic model = new Topic();
	private Long id;
	private List<Topic> list;
	private TopicService topicService;
	private CommentService commentService;
	
	private HttpServletRequest request;

	// GET /topic/1
	public HttpHeaders show() {
		return new DefaultHttpHeaders("show");
	}

	// GET /topic
	public HttpHeaders index() {
		list = topicService.getAll();
		return new DefaultHttpHeaders("index").disableCaching();
	}

	// GET /topic/1/edit
	public String edit() {
		return "edit";
	}

	// GET /topic/new
	public String editNew() {
		model = new Topic();
		return "editNew";
	}

	// GET /topic/1/deleteConfirm
	public String deleteConfirm() {
		return "deleteConfirm";
	}

	// DELETE /topic/1
	public String destroy() {
		topicService.remove(id);
		addActionMessage("Order removed successfully");
		return "success";
	}

	// POST /topic
	public HttpHeaders create() {
		model.setCreateTime(new Date());
		//User currentUser = (User) request.getSession().getAttribute("currentUser");
		//model.setUser(currentUser);
		topicService.save(model);
		addActionMessage("New order created successfully");
		return new DefaultHttpHeaders("success").setLocationId(model.getId());
	}

	// PUT /topic/1
	public String update() {
		topicService.save(model);
		addActionMessage("Order updated successfully");
		return "success";
	}

	public void validate() {
	}
	
	public void setId(Long id) {
		if (id != null) {
			this.model = topicService.get(id);
		}
		this.id = id;
	}

	public Object getModel() {
		return (list != null ? list : model);
	}

	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

}
